<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>绑定并调用闭包</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="closure.bindto.html">« Closure::bindTo</a></li>
      <li style="float: right;"><a href="closure.fromcallable.html">Closure::fromCallable »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.closure.html">Closure</a></li>
    <li>绑定并调用闭包</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="closure.call" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Closure::call</h1>
  <p class="verinfo">(PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Closure::call</span> &mdash; <span class="dc-title">绑定并调用闭包</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-closure.call-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Closure::call</strong></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$newThis</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$args</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   暂时将闭包绑定到 <code class="parameter">newThis</code>，并使用任意给定的参数调用它。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-closure.call-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">newThis</code></dt>

    <dd>

     <p class="para">
      在调用期间将闭包绑定到 <span class="type">object</span>。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">args</code></dt>

    <dd>

     <p class="para">
      零个或多个参数，他们将作为参数传递给闭包。
     </p>
    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-closure.call-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回闭包的返回值。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-closure.call-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-395">
   <p><strong>示例 #1 <span class="function"><strong>Closure::call()</strong></span> 示例</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Value&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">$three&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Value</span><span style="color: #007700">(</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$four&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Value</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$closure&nbsp;</span><span style="color: #007700">=&nbsp;function&nbsp;(</span><span style="color: #0000BB">$delta</span><span style="color: #007700">)&nbsp;{&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">()&nbsp;+&nbsp;</span><span style="color: #0000BB">$delta</span><span style="color: #007700">);&nbsp;};<br /></span><span style="color: #0000BB">$closure</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">(</span><span style="color: #0000BB">$three</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$closure</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">(</span><span style="color: #0000BB">$four</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(7)
int(8)
</pre></div>
   </div>
  </div>
 </div>

</div></div></div></body></html>